package ch.usi.da.udp;

import java.io.IOException;
import java.net.*;

/**
 * Created by IntelliJ IDEA.
 * User: Sergey Karakovskiy, sergey at idsia dot ch
 * Date: Nov 25, 2009
 * Time: 5:34:29 PM
 * Package: ch.usi.da.udp
 */
public class CustomUDPClient
{
    public void act() throws IOException, UnknownHostException
    {
        DatagramSocket socket = new DatagramSocket();
        InetAddress server = InetAddress.getByName("localhost");
        byte [] buffer = new byte[1024];
        String myMessage = "Message from Client";
        buffer = myMessage.getBytes("US-ASCII") ;

        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, server, 4224);

        socket.send(packet);
        System.out.println("Client: sent data = " + new String ( packet.getData()));
        byte[] bufferForReceive = new byte[1024];
        packet = new DatagramPacket(bufferForReceive, bufferForReceive.length);
        socket.receive(packet);
        System.out.println("Client: received data = <" + new String ( packet.getData()) + ">");
    }
}
